<?php
namespace app\bee\controller;
use think\Controller;
use \DOMDocument;
use \DateTime;

define('UPLOAD_PATH', ROOT_PATH . "public/upload/goods/");
class Pandora extends Controller
{
  const CATEGORY_ID = 849;
  const BRAND_ID = 357;
  const UPLOAD_PATH = UPLOAD_PATH;
  public function __initialize(){
    // define('CATEGORY_ID', 849);
  }
  public function index() {

    $ids = array();
    if ($_POST) {
      // var_dump($_POST);
      $ids = array_map('trim', explode("\n", $_POST['ids']));
    }
      if (is_array($ids) && !empty($ids)) {
        foreach ($ids as $item_id) {
          $data = $this->getProductInfo($item_id);
          $data['cate_id'] = self::CATEGORY_ID;
          $data['id'] = $item_id;
          $result[] = $data;
        }
      }
    
    $this->assign('result', $result);
    return view();
  }
  public function downloadImages(){
    $result = array();
    if (isset($_POST['ids'])){
      $ids = array_map('trim', explode("\n", $_POST['ids']));
      var_dump('$ids', $ids);
      foreach($ids as $item) {
        $download_report = $this->downloadImageByProductId($item);
        var_dump($download_report);
        $result['download_report'][] = $download_report;
      }      
    }
    var_dump($result);
    $this->assign('result', $result);
    return view();
  }
  /**
   * @desc 下载潘多拉官网的图片
   * @input string $id
   * @output array $result
   */
  private function downloadImageByProductId($id) {
    $date_time = new DateTime();
    $image_path = self::UPLOAD_PATH . $date_time->format('Y/m-d') . '/';
    $url = "http://www.pandora.net/assets/packshot?sku={$id}&format=jpg&width=1000&height=1000";
    $fh = fopen($url, 'r');
    $file_path = $image_path . $id . ".jpg";
    // 创建文件夹
    if (!is_dir($image_path)){
      mkdir($image_path, 0777, true);
    }
    if (!file_exists($file_path)) {
      $result['file_put_contents'] = file_put_contents($file_path, $fh);
    } 
    $result['image_url'] = substr($file_path, strlen(ROOT_PATH));
    return $result;
  }
  public function productList() {
    $url = "http://www.pandora.net/en-us/feeds/products/json";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    $result = curl_exec($ch);
    curl_close($ch);

    $obj = json_decode($result, true);
    var_dump($obj['data']['products']);
  }

  public function getProductInfo($productID) {
    $result = '';
    $url = "http://estore-us.pandora.net/on/demandware.store/Sites-en-US-Site/default/Search-GetSuggestions?q={$productID}&legacy=false";

    $ch = curl_init($url);

    //curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // var_dump($ch);
    $response = curl_exec($ch);
    var_dump($response);
    $doc = new DOMDocument();
    $doc->loadHTML($response);
    $divs = $doc->getElementsByTagName("div");
    foreach ($divs as $div) {
      if ($div->getAttribute('class') == 'search-suggestion-right-panel-product-price') {
        $result['price'] = trim(strstr($div->nodeValue, " "));
      }
    }
    foreach ($doc->getElementsByTagName('a') as $item) {
      if ($item->getAttribute('class') == 'search-suggestion-normal-link') {
        $result['name'] = $item->nodeValue;
      }
    }
//       $result['name'] = array_map(function($item){
//         if ($item->getAttribute('class') == 'search-suggestion-normal-link') {
//           $result['name'] = $time->nodeValue;
//         }
//       }, $doc->getElementsByTagName('a'));


    if(curl_errno($ch)){
        echo 'Curl error: ' . curl_error($ch);
    }

    curl_close($ch); 
    return $result;
  }
}